﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Runtime.Remoting.Messaging;
using System.Web.Http;
using System.Web.Routing;
using echarts.Controllers;
using echarts.data;
using echarts.data.DataModel;
using echarts.data.Entity;

namespace echarts.Views
{
    public class ProductController : BaseController
    {

        /// <summary>
        /// 按省分组获取所有数据
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("api/product/data")]
        public IHttpActionResult GetData()
        {
            try
            {
                var result = ProductDao.GetDataGroupByProvice();
                return Success(result);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return Failure("后台异常");
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <returns></returns>
        [HttpPost, Route("api/product/add")]
        public IHttpActionResult AddProduct(AddProductModel model)
        {
            try
            {
                if (!ModelState.IsValid
                    || string.IsNullOrEmpty(model.Province)
                    || string.IsNullOrEmpty(model.Description))
                    return Failure("参数错误");

                var add = new ProductData() 
                {
                    Province = model.Province.Trim(),
                    City = "",
                    Address = "",
                    Description = model.Description,
                    Name = "",
                    Url = ""
                };

                ProductDao.AddProductData(add);
                return Success("添加成功");
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return Failure("后台异常");
        }

        /// <summary>
        /// 删除产品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("api/product/delete")]
        public IHttpActionResult DeleteById(int id)
        {
            try
            {
                if (id <= 0)
                    return Failure("参数错误");

                var isDelete = ProductDao.DeleteById(id);

                return isDelete ? Success("操作成功") : Failure("删除失败");
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return Failure("后台异常");
        }

        /// <summary>
        /// 获取当前省份的所有产品
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("api/product/getProvinceData")]
        public IHttpActionResult GetDataByProvice(string province, int page = 1)
        {
            try
            {
                if (string.IsNullOrEmpty(province))
                    return Failure("参数错误");

                page = page < 1 ? 1 : page;

                int totalPage = 0;
                var result = ProductDao.GetListByProvince(province, page, ref totalPage);
                return Success(result);
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return Failure("后台异常");
        }
    }
}